//
//  LQYShowAllSelectionsViewController.m
//  FAKER
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "LQYShowAllSelectionsViewController.h"
#import "AllHeader.h"
#import "FitHeader.h"
@interface LQYShowAllSelectionsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)BaseClassTableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArray;
@property(nonatomic, retain)LQYItemsDetailViewController *IDVC;
@property(nonatomic, retain)YHLoadingView *loadingView;
@end

@implementation LQYShowAllSelectionsViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"%@", _carryTitleString];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataArray removeAllObjects];
        if ([_carryStyleString isEqualToString:@"all_pic"]){
            [self getOnlyPicData];
        }
        else{
            [self getAllActivityData];
        }
    }];
    
    
    _loadingView = [[YHLoadingView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    if (NSUSERDEFAULTS_NIGHT) {
        _loadingView.backgroundColor = [UIColor blackColor];
    }
    else{
        _loadingView.backgroundColor = [UIColor whiteColor];
    }
    [self.view addSubview:_loadingView];
    
}
-(void)loadView
{
    [super loadView];
    self.dataArray = [NSMutableArray array];
#pragma mark    如果key為all_pic則請求all_pic類數據
    if ([_carryStyleString isEqualToString:@"all_pic"]){
        [self getOnlyPicData];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [_dataArray removeAllObjects];
            [self getOnlyPicData];
        }];
    }
    else{
        [self getAllActivityData];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [_dataArray removeAllObjects];
            [self getOnlyPicData];
        }];
    }
    _tableView = [[BaseClassTableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = 0;
    [self.view addSubview:_tableView];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_carryStyleString isEqualToString:@"all_pic"])
    {
        return 212.5 * FITHEIGHT;
    }
    else
    {
        return 320 * FITHEIGHT;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifie = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifie];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifie];
    }
    LQYFirstPageOnlyImageTableViewCell *onlyImageCell = [[LQYFirstPageOnlyImageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifie];
    LQYFirstPageImageAndLabelTableViewCell *imageAndLabelCell = [[LQYFirstPageImageAndLabelTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifie];
#pragma mark    判斷自定義cell應為什麼樣式
    if ([_carryStyleString isEqualToString:@"all_pic"]){
        [onlyImageCell.backImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[_dataArray[indexPath.row] valueForKey:@"pic"] valueForKey:@"url"]]]placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
        cell = onlyImageCell;
    }
    else{
        if ([_carryTypeString isEqualToString:@"items"]) {
            [imageAndLabelCell.topImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[_dataArray[indexPath.row] valueForKey:@"pic"] valueForKey:@"url"]]]placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
            imageAndLabelCell.titleLabel.text = [_dataArray[indexPath.row] valueForKey:@"title"];
            imageAndLabelCell.introduceLabel.text = [_dataArray[indexPath.row] valueForKey:@"content"];
        }
        else{
            [imageAndLabelCell.topImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[[_dataArray[indexPath.row] valueForKey:@"thumbnail_medias"] objectAtIndex:0] valueForKey:@"url"]]]placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
            imageAndLabelCell.titleLabel.text = [_dataArray[indexPath.row] valueForKey:@"title"];
            imageAndLabelCell.introduceLabel.text = [_dataArray[indexPath.row] valueForKey:@"pos_str"];
        }
        cell = imageAndLabelCell;
    }
    cell.selectionStyle = 0;
    return cell;
}
#pragma mark    數據獲取
-(void)getOnlyPicData
{
    [InternetDataRequest getDataByString:_carryLinkString BodyDic:nil WithDataBlock:^(id data) {
        NSDictionary *dic = [data valueForKey:@"data"];
        NSArray *array = [dic objectForKey:@"items"];
        for (NSDictionary *dic in array)
        {
            LQYChannelDetailItemModal *modal = [[LQYChannelDetailItemModal alloc]init];
            [modal setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:modal];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [_loadingView removeFromSuperview];
    }];
}
-(void)getAllActivityData
{
    [InternetDataRequest getDataByString:_carryLinkString BodyDic:nil WithDataBlock:^(id data) {
        NSDictionary *dic = [data valueForKey:@"data"];
        NSArray *array = [dic objectForKey:[NSString stringWithFormat:@"%@", _carryTypeString]];
        for (NSDictionary *dic in array)
        {
            //根據TypeString值判斷用何種modal接收
            if ([_carryTypeString isEqualToString:@"items"]){
                LQYChannelDetailItemModal *modal1 = [[LQYChannelDetailItemModal alloc]init];
                [modal1 setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:modal1];
            }
            else{
                LQYALLActivityItemsModal *modal = [[LQYALLActivityItemsModal alloc]init];
                [modal setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:modal];
            }
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [_loadingView removeFromSuperview];
    }];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _IDVC = [[LQYItemsDetailViewController alloc]init];
    //设置count表示key的多少并根据key的值来判断传何值
        //根据value的类型来确定跳转页面时传的值
    if ([_carryTypeString isEqualToString:@"items"]) {
        if ([_carryRankNumber isEqualToString:@"70"]) {
            _IDVC.carryAdUrl = [[_dataArray[indexPath.row]valueForKey:@"web"]valueForKey:@"url"];
        }
        else{
            NSDictionary *modalDic = [_dataArray[indexPath.row]valueForKey:@"article"];
            LQYJudgeArticleModal *modal = [[LQYJudgeArticleModal alloc] init];
            [modal setValuesForKeysWithDictionary:modalDic];
            _IDVC.carryTitleString = [_dataArray[indexPath.row]valueForKey:@"title"];
            _IDVC.carryAutherString =[[_dataArray[indexPath.row]valueForKey:@"article"]valueForKey:@"auther_name"];
            _IDVC.count = [[_dataArray[indexPath.row]valueForKey:@"article"]allKeys].count;
            _IDVC.articleModal = modal;

        }
    }
    else{
        _IDVC.carryWeekendUrl = [[_dataArray[indexPath.row]valueForKey:@"weekend"]valueForKey:@"content_url"];
        _IDVC.tag = 1;
    }
    _IDVC.modalTransitionStyle = 2;
    [self presentViewController:_IDVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
